//
//  BySetPayPasswardSetp2VC.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/24.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "BySetPayPasswardSetp2VC.h"

// vc
#import "BySetPayPasswardSetp3VC.h"

@interface BySetPayPasswardSetp2VC ()

@end

@implementation BySetPayPasswardSetp2VC
{
    UILabel     *_pwNoteLabel;          //
    UITextField *_pwTf;                 // 密码输入
    
    UILabel     *_pwConfirmNoteLabel;   //
    UITextField *_pwConfirmTf;          // 密码确认
    
    UILabel     *_errorLabel;           // 错误提示
    
    UIButton    *_nextButton;           // 下一步
}
#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"设置交易密码";
    self.view.backgroundColor = BY_BG_COLOR;
    
    [self addLeftBackBarButtonItem];
    
    [self createViews];
}


#pragma mark - 创建视图
- (void)createViews {
    
    //
    _pwNoteLabel = [[UILabel alloc] init];
    _pwNoteLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    _pwNoteLabel.adjustsFontSizeToFitWidth = true;
    _pwNoteLabel.numberOfLines = 1;
    _pwNoteLabel.text = @"交易密码";
    _pwNoteLabel.font = FONT(18);
    _pwNoteLabel.textAlignment = NSTextAlignmentCenter;
    _pwNoteLabel.textColor = [UIColor whiteColor];
    _pwNoteLabel.layer.masksToBounds = true;
    _pwNoteLabel.layer.cornerRadius = 4;
    [self.view addSubview:_pwNoteLabel];
    
    // 密码
    _pwTf = [UITextField new];
    _pwTf.layer.cornerRadius = 4;
    _pwTf.backgroundColor = [UIColor whiteColor];
    _pwTf.placeholder = @"请输入6位数字交易密码";
    _pwTf.font = FONT(18);
    _pwTf.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_pwTf];
    
    //
    _pwConfirmNoteLabel = [[UILabel alloc] init];
    _pwConfirmNoteLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    _pwConfirmNoteLabel.adjustsFontSizeToFitWidth = true;
    _pwConfirmNoteLabel.numberOfLines = 1;
    _pwConfirmNoteLabel.text = @"交易密码确认";
    _pwConfirmNoteLabel.font = FONT(18);
    _pwConfirmNoteLabel.textAlignment = NSTextAlignmentCenter;
    _pwConfirmNoteLabel.textColor = [UIColor whiteColor];
    _pwConfirmNoteLabel.layer.masksToBounds = true;
    _pwConfirmNoteLabel.layer.cornerRadius = 4;
    [self.view addSubview:_pwConfirmNoteLabel];
    
    // 确认密码
    _pwConfirmTf = [UITextField new];
    _pwConfirmTf.layer.cornerRadius = 4;
    _pwConfirmTf.backgroundColor = [UIColor whiteColor];
    _pwConfirmTf.placeholder = @"请确认6位数字交易密码";
    _pwConfirmTf.font = FONT(18);
    _pwConfirmTf.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_pwConfirmTf];
    
    //
    _errorLabel = [[UILabel alloc] init];
    _errorLabel.backgroundColor = [UIColor whiteColor];
    _errorLabel.adjustsFontSizeToFitWidth = true;
    _errorLabel.numberOfLines = 1;
    _errorLabel.text = @"密码不匹配，请重新输入";
    _errorLabel.font = FONT(18);
    _errorLabel.textAlignment = NSTextAlignmentCenter;
    _errorLabel.textColor = BY_RED_COLOR;
    _errorLabel.layer.borderWidth = 1;
    _errorLabel.layer.borderColor = BY_RED_COLOR.CGColor;
    _errorLabel.layer.masksToBounds = true;
    _errorLabel.layer.cornerRadius = 4;
    [self.view addSubview:_errorLabel];
    
    // 下一步
    _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _nextButton.titleLabel.font = FONT(18);
    _nextButton.backgroundColor = BY_THEME_COLOR;
    _nextButton.layer.cornerRadius = 4;
    [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextButton setTitleColor:BY_GOLD_COLOR forState:UIControlStateNormal];
    [_nextButton addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextButton];
    
    
    [self.view bringSubviewToFront:_pwNoteLabel];
    [self.view bringSubviewToFront:_pwConfirmNoteLabel];
    _errorLabel.hidden = true;
    
    // 布局
    [_pwNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@50);
        make.left.equalTo(self.view).offset(15);
        make.top.equalTo(self.view).offset(10);
        make.right.equalTo(self.view).offset(-15);
    }];
    [_pwTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_pwNoteLabel);
        make.top.equalTo(_pwNoteLabel.mas_bottom).offset(-2);
        make.height.equalTo(_pwNoteLabel);
    }];
    [_pwConfirmNoteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_pwTf);
        make.height.equalTo(_pwTf);
        make.top.equalTo(_pwTf.mas_bottom).offset(10);
    }];
    [_pwConfirmTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_pwConfirmNoteLabel);
        make.top.equalTo(_pwConfirmNoteLabel.mas_bottom).offset(-2);
        make.height.equalTo(_pwConfirmNoteLabel);
    }];
    [_errorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_pwConfirmTf);
        make.height.equalTo(_pwConfirmTf);
        make.top.equalTo(_pwConfirmTf.mas_bottom).offset(10);
    }];
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(_errorLabel);
        make.top.equalTo(_errorLabel.mas_bottom).offset(50);
    }];
}

#pragma mark - 点击事件
- (void)nextStep {
    
    if (_pwTf.text.length <= 0 || _pwConfirmTf.text.length <= 0) {
        
        _errorLabel.text = @"密码不能为空，请重新输入";
        _errorLabel.hidden = false;
        _errorLabel.alpha = 1;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:1 animations:^{
                _errorLabel.alpha = 0;
            } completion:^(BOOL finished) {
                _errorLabel.hidden = true;
            }];
        });
        
        return;
    }
    
    if (![_pwTf.text isEqualToString:_pwConfirmTf.text]) {
        _errorLabel.text = @"密码不匹配，请重新输入";
        _errorLabel.hidden = false;
        _errorLabel.alpha = 1;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:1 animations:^{
                _errorLabel.alpha = 0;
            } completion:^(BOOL finished) {
                _errorLabel.hidden = true;
            }];
        });
        
        return;
    }
    
    [self by_normalPush:[BySetPayPasswardSetp3VC new] hasNavi:true];
}

@end
